package com.miao.teacher.ui;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

import com.espeaker.sdk.Constants;
import com.espeaker.sdk.api.LoginApi;
import com.espeaker.sdk.utils.JsonUtils;
import com.miao.teacher.app.TApp;
import com.miao.teacher.utils.FileUtil;
import com.miao.teacher.utils.LoginUtil;
import com.miao.teacher.utils.Utils;
import com.miao.teacher.widget.WaitingDialog;

import android.content.Context;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends BaseActivity {

	private LoginApi api = new LoginApi();
	private String phoneNum = "";
	private String passWord = "";

	private WaitingDialog loginDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_login);
		loginDialog = new WaitingDialog(this, "登录中...");
		initView();
	}

	@Override
	protected void RecMsgFromHandler(Message msg) {
		// TODO Auto-generated method stub
		super.RecMsgFromHandler(msg);
		switch (msg.what) {
		case Constants.LOGINAPI_LOGININ:
			loginFinish(msg.obj);
			break;
		}
	}

	/*
	 * 初始化控件
	 */
	private void initView() {
		((Button) findViewById(R.id.btnLogin))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						doLogin();
					}
				});
		String userName = FileUtil.getPreferences(this, TApp.Account_KEY);
		String pass = FileUtil.getPreferences(this, TApp.PASSWORD_KEY);
		if (userName != null) {
			((EditText) findViewById(R.id.edt_account)).setText(userName);
		}
		if (pass != null) {
			((EditText) findViewById(R.id.edt_password)).setText(pass);
		}
		((Button) findViewById(R.id.btnLogin)).postDelayed(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				InputMethodManager imm = (InputMethodManager) getApplicationContext()
						.getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
			}

		}, 300);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		// JPushInterface.onPause(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// JPushInterface.onResume(this);
	}

	/*
	 * 登陆
	 */
	private void doLogin() {
		phoneNum = ((EditText) findViewById(R.id.edt_account)).getText()
				.toString();
		passWord = ((EditText) findViewById(R.id.edt_password)).getText()
				.toString();
		if (!LoginUtil.checkMobile(phoneNum)) {
			ToastShowMsg(R.string.str_login_account_error);
			return;
		}
		if (!LoginUtil.checkLoginPassword(passWord)) {
			ToastShowMsg(R.string.str_login_password_empty);
			return;
		}
		Utils.showProgress(loginDialog, true);
		api.userLogin(phoneNum, passWord, mHandler);
	}

	/*
	 * 登陆消息返回
	 */
	private void loginFinish(Object obj) {
		try {
			Utils.showProgress(loginDialog, false);
			JSONObject json = new JSONObject(obj.toString());
			int status = JsonUtils.getInt(json, "status", -1);
			if (status == 404) {
				ToastShowMsg(R.string.str_login_account_noexit);
			} else if (status == 403) {
				ToastShowMsg(R.string.str_login_password_error);
			} else if (status == 0) {
				ToastShowMsg(R.string.str_login_success);
				JSONObject json2 = JsonUtils
						.getJSONObject(json, "values", null);
				int id = JsonUtils.getInt(json2, "reviser_id", -1);
				FileUtil.addPreferences(LoginActivity.this, TApp.USER_ID, id
						+ "");
				FileUtil.addPreferences(LoginActivity.this, TApp.Account_KEY,
						phoneNum);
				FileUtil.addPreferences(LoginActivity.this, TApp.PASSWORD_KEY,
						passWord);
				MiaoActivity.createInstance(LoginActivity.this);
				finish();
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
